# -*- coding:utf-8 -*-

import os
import re
import xml.etree.ElementTree as ET
import commands
from HostIp import host_ip


def appinfo():
    record_host = host_ip()
    path = '/data/Application/'
    apps = []
    if os.path.exists(path):
        for p in os.listdir(path):
            jvm_conf = path + p + '/bin/catalina.sh'
            if os.path.isfile(p):
                pass
            elif not os.path.isfile(jvm_conf):
                pass
            else:
                apps.append(p)

        app_list = []
        for ap in apps:
            some_app = dict()
            app_status = dict()
            some_app['app_name'] = ap
            some_app["ipaddr"] = record_host
            grep_res = commands.getoutput("ps -ef | grep %s"%ap)
            content = grep_res.split("\n")
            # print "ccccc",content
            '''
            (0,"online"),(1,"offline")
            '''
            if len(content) == 3:
                app_status["status"] = 0
            else:
                app_status["status"] = 1

            some_app.update(app_status)

            jvm_conf = path + ap +'/bin/catalina.sh'
            server_conf = path + ap +'/conf/server.xml'


            # 处理 jvm_conf 文件，提取jvm_port
            # 把文章中带有# 注释的行，全部删除
            with open(jvm_conf,mode="r") as f:
                with open("jvm_swap.conf",mode="w") as write_f:
                    for line in f:
                        if line == '\n':
                            # print "sssss", line
                            pass
                        elif re.findall("[' ']{0,}#",line) or not line:
                            pass
                        else:
                            write_f.write(line)
            aa = None
            with open('jvm_swap.conf',mode="r") as f:
                for line in f:
                    if 'CATALINA_OPTS' in line:
                        aa = line
                        break
            # print "aaa",aa

            aa_list = aa.split(" ")
            jvm_port = None
            for it in aa_list:
                if 'jmxremote.port' in it:
                    jvm_port = it.strip().rsplit("=")[1]
                    break
                else:
                    jvm_port = "None"
            some_app["jvm_port"] = jvm_port
            some_app["app_type"] = "tomcat"

            os.remove('jvm_swap.conf')

            # 获取shutdown 端口
            tree = ET.parse(server_conf)
            tree.write("%s.xml"%ap)

            shutdown_port = None
            conn_port = None

            context_path = None
            work_path = None
            urls = dict()
            with open("%s.xml"%ap, 'r') as f:
                for line in f:
                    # print line
                    if "shutdown" in line:
                        for s in line.split():
                            if "port" in s:
                                # print "1111",s.split("=")[1]
                                # print "2222",s.split("=")[1].strip('"')
                                shutdown_port = s.split("=")[1].strip('"')
                    if 'Connector' in line:
                        # print "Connector",line
                        # print line.split()
                        for p in line.split():
                            if "port" in p:
                                conn_port = p.split("=")[1].strip('"')
                    if "Context" in line:
                        # print "Context", line
                        for c in line.split():
                            if 'docBase' in c:
                                # print "ddd", c
                                work_path = c.split("=")[1].strip('"')

                            if 'path' in c:
                                # print "pppp", c
                                context_path = c.split("=")[1].strip('"')

                        urls[context_path] = work_path

            some_app["shutdown_port"] = shutdown_port
            some_app["conn_port"] = conn_port

            some_app["path"] = urls
            app_list.append(some_app)
            os.remove("%s.xml"%ap)
    else:
        app_list = "no_application"
    return app_list

